<?php
class Core_Controller_Router_Route_Lang extends Zend_Controller_Router_Route_Abstract {
	const URI_DELIMITER = '/';
	
	protected $_defaultLang = null;
	protected $_defaults = array ();
	protected $_langList = null;
	protected $_paramName = null;
	protected $_lang = null;
	
	public static function getInstance(Zend_Config $config) {
		$frontController = Zend_Controller_Front::getInstance ();
		
		$defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray () : array ();
		$dispatcher = $frontController->getDispatcher ();
		$request = $frontController->getRequest ();
		
		return new self ( $defs, $dispatcher, $request );
	}
	
	public function getVersion() {
		return 1;
	}
	
	public function __construct($paramName, $defaultLang, $langList) {
		$this->_paramName = $paramName;
		$this->_defaultLang = $defaultLang;
		$this->_defaults [$this->_paramName] = $defaultLang;
		$this->_langList = $langList;
	}
	
	public function match($path, $partial = false) {
		$value [$this->_paramName] = $this->_defaultLang;
		$matchedPath = null;
		
		list ( $lang ) = explode ( self::URI_DELIMITER, $path );
		
		if (in_array ( $lang, $this->_langList )) {
			$value [$this->_paramName] = $lang;
			$matchedPath = $lang;
			$this->_lang = $lang;
		}
		
		$this->setMatchedPath ( $matchedPath );
		
		return $value;
	}
	
	public function assemble($data = array(), $reset = false, $encode = false, $partial = false) {
		return $this->_lang;
	}
	
	public function getDefault($name) {
		if (isset ( $this->_defaults [$name] )) {
			return $this->_defaults [$name];
		}
		return null;
	}
	
	public function getDefaults() {
		return $this->_defaults;
	}
}